package com.boco.webmaster.base;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class CharsetFilter extends HttpServlet implements Filter {
    private FilterConfig filterConfig;
    private String charset;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.charset = filterConfig.getInitParameter("charset");
    }

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain filterChain) {
        try {
            if(this.charset != null && this.charset.trim().length() > 0){
                request.setCharacterEncoding(this.charset);
            }
            filterChain.doFilter(request, response);
        } catch (ServletException sx) {
            filterConfig.getServletContext().log(sx.getMessage());
        } catch (IOException iox) {
            filterConfig.getServletContext().log(iox.getMessage());
        }
    }

    //Clean up resources
    public void destroy() {
    }
}
